#!/usr/bin/env bash

function CheckVariableType(){
    local a="${1}"
    printf "%d" "$a" &>/dev/null && echo "INTEGER" && return
    printf "%d" "$(echo $a|sed 's/^[+-]\?0\+//')" &>/dev/null && echo "INTEGER" && return
    printf "%f" "$a" &>/dev/null && (if [[ `echo ${a} | grep [E,e]` == "" ]]; then echo "DOUBLE"; else echo "EXPONENT"; fi;) && return
    [ ${#a} -eq 1 ] && echo "CHAR" && return
    echo "STRING"
}

function FormatVariable(){
    if [[ $# -ge 2 ]]; then
        printf "${2}" ${1} 
    else
        vType=`CheckVariableType ${1}`
        if [[ ${vType} == "INTEGER" ]]; then
            printf "%d\n" $1
        elif [[ ${vType} == "DOUBLE" ]]; then
            printf "%f\n" $1
        elif [[ ${vType} == "EXPONENT" ]]; then
            printf "%E\n" $1
        else
            printf "\"%s\"\n" $1
        fi
    fi
}

function changeValue(){
    sed -i "/^${1}/c${1} = ${2}" $3
}

function Value(){
    val=$(sed -n "/^\<${1}\>/p" ${2} | cut -d "=" -f 2 | sed "s/ //g" | sed "s/\"//g")
    FormatVariable "${val}"
}
